﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCBugTracker.Models
{
    public class EFProfileRepository : IProfileRepository
    {
        MVCBUGTRACKEREntities db = new MVCBUGTRACKEREntities();

        public UserProfile GetProfile(string userName)
        {
            var profile = (from p in db.UserProfiles
                           where p.UserName == userName
                           select p).SingleOrDefault();
            if (profile != null)
                return profile;
            else
            {
                profile = new UserProfile() { UserName = userName };
                AddProfile(profile);
                return profile;
            }
        }

        public void AddProfile(UserProfile profile)
        {
            db.UserProfiles.AddObject(profile);
        }

        public void DeleteProfile(UserProfile profile)
        {
            db.UserProfiles.DeleteObject(profile);
        }

        public void Save()
        {
            db.SaveChanges();
        }
    }
}